<script setup>
	import { Edit, Plus} from "@element-plus/icons-vue";
	import {userApi} from "@/api/user.js";
	import {useUserInfoStore} from '@/store/userInfo.js'
	import {ref} from "vue";
	import {ElMessage} from "element-plus";
	import {deptApi} from "@/api/dept.js";
	
	const userInfoStore = useUserInfoStore();
	const user = ref({})
	const dept=ref({})
	const loadInfo = () => {
		Object.assign(user.value,userInfoStore.user)
		deptApi.selectById(user.value.departmentId).then(result=>{
			dept.value=result.data
		})
	}
	loadInfo()
	//编辑
	const dialogVisible = ref(false)
	const showUpdateDialog = (id) => {
		dialogVisible.value = true
		Object.assign(user.value,userInfoStore.user)
		selectByPId(userInfoStore.user.provinceId)
		selectByCId(userInfoStore.user.cityId)
	}
	
	const toUpdate = () => {
		userApi.update(user.value).then(result => {
			if (result.code === 0) {
				ElMessage({message: result.msg, type: 'success',})
				dialogVisible.value = false  //弹层消失
				userApi.selectById(user.value.id).then(result=>{
					userInfoStore.setUserInfo(result.data)
					loadInfo()
				})
			} else {
				ElMessage.error(result.msg)
			}
			
		})
		
	}
	
	import {useTokenStore} from "@/store/token.js";
	import {addressApi} from "@/api/address.js";
	const tokenStore = useTokenStore();
	const headers = ref({
		//携带token传到后端
		Authorization: tokenStore.token
	});
	const handleAvatarSuccess = (result) => {
		user.value.image = result.data
	}
	
	//省份数据
	const pList = ref([])
	const cList = ref([])
	const aList = ref([])
	//获取分类数据
	const selectAllProvince = () => {
		addressApi.selectProvince().then(result => {
			pList.value = result.data
		})
	}
	selectAllProvince()
	const selectByPId = (value) => {
		if(value){
			addressApi.selectByPId(value).then(result => {
				cList.value = result.data
			})
			
		}
	}
	const selectByCId = (value) => {
		if(value){
			addressApi.selectByCId(value).then(result => {
				aList.value = result.data
			})
		}
	}
</script>

<template>
	<el-card class="">
		<el-descriptions border style="font-size:32px">
			<el-descriptions-item
				:rowspan="2"
				:width="160"
				label="头像"
				align="center"
				style="font-size:20px"
			>
				<el-image
					style="width: 100px; height: 100px"
					v-if="user.image" :src="user.image"
				/>
			</el-descriptions-item>
			<el-descriptions-item label="用户名" style="font-size: 20px" align="center">{{ user.name }}</el-descriptions-item>
			<el-descriptions-item label="性别" align="center">{{ user.gender }}</el-descriptions-item>
			<el-descriptions-item label="联系方式" align="center">{{ user.phone }}</el-descriptions-item>
			<el-descriptions-item label="生日" align="center">{{ user.birthDate }}</el-descriptions-item>
			<el-descriptions-item label="部门" align="center">{{ dept.name }}</el-descriptions-item>
<!--			<el-descriptions-item label="部门" align="center">{{ user.departmentId }}</el-descriptions-item>-->
			<el-descriptions-item label="邮箱" align="center" :height="50">
				<el-tag size="large">{{ user.email }}</el-tag>
			</el-descriptions-item>
			<el-descriptions-item label="QQ号" align="center">{{ user.qqId }}</el-descriptions-item>
			<el-descriptions-item label="地址" align="center" :span="3">{{ user.address }}</el-descriptions-item>
			<el-descriptions-item label="操作" align="center" :span="3">
				<el-button type="primary" :icon="Edit" round @click="showUpdateDialog(user.id)">编辑</el-button>
			</el-descriptions-item>
		</el-descriptions>
	</el-card>
	
	<el-dialog v-model="dialogVisible" title="编辑" width="800" :lock-scroll="false">
		<el-form :model="user">
			<el-form-item label="姓&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;名：" :label-width="100">
				<el-input v-model="user.name" autocomplete="off"/>
			</el-form-item>
			<el-form-item label="性&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;别：" :label-width="100">
				<el-radio-group v-model="user.gender">
					<el-radio value="男" size="large" border>男</el-radio>
					<el-radio value="女" size="large" border>女</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="生&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;日：" :label-width="100">
				<div class="block">
					<el-date-picker v-model="user.birthDate" type="date" placeholder="选择日期"
									value-format="YYYY-MM-DD" :size="size"/>
				</div>
			</el-form-item>
			<el-form-item label="地&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;址：" :label-width="100">
				<el-select v-model="user.provinceId" class="m-2"
						   clearable
						   placeholder="请选择省份"
						   @change='selectByPId'
						   ar='clearSelect' style='width:200px'>
					<el-option
						v-for="item in pList"
						:key="item.id"
						:label="item.name"
						:value="item.id"
					/>
				</el-select>
				<el-select v-model="user.cityId" class="m-2"
						   clearable
						   placeholder="请选择城市"
						   @change='selectByCId'
						   style='width:200px'>
					<el-option
						v-for="item in cList"
						:key="item.id"
						:label="item.name"
						:value="item.id"
					/>
				</el-select>
				<el-select v-model="user.areaId" class="m-2"
						   clearable
						   placeholder="请选择区县"
						   ar='clearSelect' style='width:200px'>
					<el-option
						v-for="item in aList"
						:key="item.id"
						:label="item.name"
						:value="item.id"
					/>
				</el-select>
			</el-form-item>
			<el-form-item label="联系方式：" :label-width="100">
				<el-input v-model="user.phone" autocomplete="off"/>
			</el-form-item>
			<el-form-item label="邮&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;箱：" :label-width="100">
				<el-input v-model="user.email" autocomplete="off"/>
			</el-form-item>
			<el-form-item label="Q&nbsp;&nbsp;Q&nbsp;&nbsp;&nbsp;号：" :label-width="100">
				<el-input v-model="user.qqId" autocomplete="off"/>
			</el-form-item>
			<el-form-item label="头&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;像：" :label-width="100">
				<el-upload
					class="avatar-uploader"
					action="/api/upload"
					:show-file-list="false"
					:on-success="handleAvatarSuccess"
					:headers="headers"
				>
					<img v-if="user.image" :src="user.image" class="avatar"/>
					<el-icon v-else class="avatar-uploader-icon">
						<Plus/>
					</el-icon>
				</el-upload>
			</el-form-item>
		</el-form>
		<template #footer>
			<div class="dialog-footer">
				<el-button @click="dialogVisible = false">取消</el-button>
				<el-button type="primary" @click="toUpdate">
					确认修改
				</el-button>
			</div>
		</template>
	</el-dialog>

</template>

<style scoped>
	.avatar-uploader .avatar {
		width: 178px;
		height: 178px;
		display: block;
	}
</style>
<style>
	.avatar-uploader .el-upload {
		border: 1px dashed var(--el-border-color);
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
		transition: var(--el-transition-duration-fast);
	}
	
	.avatar-uploader .el-upload:hover {
		border-color: var(--el-color-primary);
	}
	
	.el-icon.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		text-align: center;
	}
</style>